﻿
var IdNumber = {
    model: function (d = {}) {
        this.Title = d.Title || "证件号码";
        this.IdType = d.IdType || "身份证";
        this.EnTitle = d.EnTitle || "";
        this.Tips = d.Tips || "";
        this.Name = d.Name || 'lg' + lg.GetSN(6, 10);
        this.Value = d.Value || "";
        this.ExportName = d.ExportName || "";
        this.Width = d.Width || "100%";
        this.Required = d.Required || false;
        this.IsRender = d.IsRender != undefined ? d.IsRender : true;
        this.IsShow = d.IsShow || false;
        this.Group = d.Group || "";
        this.IsExport = d.IsExport || false;
        this.IsWhere = d.IsWhere || false;
        this.WhereKey = d.WhereKey || "精确匹配"
    },
    component: {
        name: "lg-form-idnumber",
        data: {
            props: {
                items: {
                    type: Array,
                    default: []
                },
                value: {
                    type: [String, Object],
                    default: ""
                },
                data: {
                    type: Object,
                    default: {}
                },
                renderType: {
                    type: Number,
                    default: 0
                }
            },
            template: `			
             <div :class="['lg-form-item','lg-form-fa-item',{'lg-ver-error':data.error},{'pointer-none':renderType==1}]" :style="{'width':data.Width}">
			   <div class="lg-form-block">
			     <div class="lg-form-title">
			       <div class="lg-form-title-item">
			         <i v-if="data.Required">*</i>
                     <span class="cn">{{data.Title}}</span>
                     <span class="en">{{data.EnTitle}}</span>
			       </div>
			     </div>
			     <div class="lg-form-control lg-form-mark">
			       <span class="lg-form-mark-icon"><i class="fa fa-id-card-o"></i></span>
			       <input type="text" class="lg-input" :value="value" @input="$emit('update:value', $event.target.value)" v-on:blur="$emit('done',data)">
			     </div>
			   </div>
               <div class="lg-form-block">
                  <div class="lg-form-tip"><p>{{data.Tips}}</p></div>
			      <div class="lg-form-ver" v-if="data.error">{{data.errMsg}}</div>
               </div>
			 </div>              
            `
        }
    }
};

IdNumber.model.prototype.verify = function () {
    var required = lg.regx.required(this.Value);
    if (this.Required && !required) {
        return { state: false, msg: "必填项不可为空!" };
    }
    if (required && this.IdType.indexOf("身份证") > -1 && !lg.regx.idCard(this.Value)) {
        return { state: false, msg: "不是有效的身份证号码！" };
    }
    return { state: true, msg: "" };
};

IdNumber.model.prototype.getValue = function () {
    var obj = { type: this.IdType, value: this.Value }
    return obj;
};

IdNumber.model.prototype.setValue = function (data) {
    var value = data[this.Name];
    if (value) {
        if (typeof value != "object") {
            value = lg.convert.toJson(value, null);
        }
        if (value != null) {
            this.Value = value.value;
            this.IdType = value.type;
        } else {
            this.Value = value;
        }
    }
};
export { IdNumber }